<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CarModel;
use app\admin\model\College;
use app\admin\model\UserModel;
use app\common\controller\Adminbase;
use app\common\model\AliPay;
use app\common\model\FishLoc;
use app\common\model\LocVideo;
use app\common\model\MemberModel;
use app\common\model\RebateLog;
use util\Tree;


class Rebate extends Adminbase
{

    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new RebateLog();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
            $date = date('Y-m-d');
            $list = $this->modelClass
                ->with(['user', 'alipay'])
                ->where($where)
//                ->where('day','<',$date)
                ->group('rebate_log.uid, rebate_log.day')
                ->field('count(rebate_log.id) as video_num, sum(rebate_log.money) as total_money,rebate_log.money')
                ->order('status asc,day desc,video_num desc')
                ->paginate($limit);
            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        return $this->fetch();
    }


    public function check()
    {

        if ($this->request->isPost()) {
            $params = $this->request->param('');
            $rebate = $this->modelClass->where(['id' => $params['id']])->find();
            try {
                (new RebateLog())->where(['uid' => $rebate['uid'], 'day' => $rebate['day']])->update(['status' => 1]);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("结算成功！");
        }
    }


    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        $rebate = $this->modelClass->with('user1')->where('id', $id)->find();

        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
            $list = RebateLog::
                where($where)
                ->where(['uid' => $rebate['uid'], 'day' => $rebate['day']])
                ->order('create_time desc')
                ->paginate($limit);

//            $list = RebateLog::
//            with('videoUser')
//                ->where($where)
//                ->order('day desc, status asc')
//                ->paginate($limit);
            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }

        //支付宝账号
        $alipay = (new AliPay())->where(['uid' => $rebate['uid']])->field('real_name,account,create_time')->find();

        //总收入和总次数
        $rebateNum = (new RebateLog())->where(['uid' => $rebate['uid'], 'day' => $rebate['day']])
            ->group('day')
            ->field('COUNT(id) as video_num,SUM(money) as total_money')
            ->find();


        //查询详情
        $this->assign('data', $rebate);
        $this->assign('alipay', $alipay);
        $this->assign('rebateNum', $rebateNum);
        return $this->fetch();
    }


}
